#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：zhcx-service-test 
@File    ：initialize_strategy_service.py
@Author  ：关依林
@Date    ：2023/7/17 23:28 
'''
import os
from utils.mysqlUtils.db_utils import DB
from common.dir_config import ConfigHandler
from utils.readFileUtils.yamlControl import GetYamlData
from testcases.ProductGrading_Strategy.public_dispatch_way import *
from utils.logging_tool.log_control import *


class PolicyConditionMethod:

    def __init__(self,categoryId):
        self.categoryId = str(categoryId)
        self.strategyConfiguration = GetYamlData(file_dir=ConfigHandler.strategy_dir + "strategy_case03.yaml").get_yaml_data()

    def strategyDataVerify(self):

        name = self.strategyConfiguration["name"]
        categoryId = int(self.strategyConfiguration["category_id"].replace("$categoryId",self.categoryId))
        dbNameProduct = self.strategyConfiguration["db_name1"]
        dbNameCategory = self.strategyConfiguration["db_name3"]
        comparison = self.strategyConfiguration["comparison"]

        useLogHandler(abnormalType = "INFO", information = "开始执行策略======================>" + name)
        messageList = getStrategyRule(categoryId)

        if messageList:
            matchingSku,errorSku = (), ()
            count = 0
            for condition in messageList:
                if not count:
                    initialMatching,initialError = (),()
                    matchingSku,errorSku = getBasicsData(dbNameCategory,dbNameProduct,condition,messageList,initialMatching,initialError)
                    count += 1
                else:
                    tempMatching,tempError = (), ()
                    tempMatching,tempError = getBasicsData(dbNameCategory,dbNameProduct, condition, messageList, tempMatching, tempError)
                    matchingSku = tuple(set(matchingSku) & set(tempMatching))
                    errorSku = tuple(set(errorSku) & set(tempError))
        else:
            useLogHandler(abnormalType="ERROR", information="策略：" + name + ",没有配置策略条件,校验异常...")
            return

        strategyResult = getStrategyResult(comparison, categoryId)
        matchingSku,errorSku = set(matchingSku),set(errorSku)
        strategyResult = set(strategyResult)
        # 取满足条件SKU与策略关联SKU的非交集
        matchingResult = tuple(matchingSku - strategyResult)
        # 取不满足条件SKU与策略关联SKU的交集
        unmatchedResult = tuple(errorSku & strategyResult)

        for err in matchingResult:
            useLogHandler("ERROR").error("sku：" + err[0] + ",该sku与策略服务执行结果不匹配")
        useLogHandler("INFO").info("结束执行策略======================>" + name)
        # return verifyResult



if __name__ == '__main__':
    PolicyConditionMethod(categoryId=88).strategyDataVerify()
    # getBasicsData("sku,last_buy_price")